//
//  BaseVC.m
//  MexicoLoan
//
//  Created by WXP on 2025/2/24.
//
//

#import "BaseVC.h"
#import "BaseVC+KeyboardObserve.h"

@interface BaseVC ()

@end

@implementation BaseVC

#pragma mark - lazy
- (UIView *)viewBG{
    if (!_viewBG) {
        _viewBG = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _viewBG.backgroundColor = [UIColor whiteColor];
    }
    return _viewBG;
}
- (LoadingView *)loadingView{
    if (_loadingView == nil) {
        _loadingView = [LoadingView new];
    }
    return _loadingView;
}
- (NoticeView *)noticeView{
    if (_noticeView == nil) {
        _noticeView = [NoticeView new];
    }
    return _noticeView;
}

#pragma mark - viewDidLoad
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = false;
    if (self.isKeyboardObserve) {
        [self addKeyboardObserve];
    }
#ifdef DEBUG
    NSLog(@"current vc - %@",NSStringFromClass(self.class));
#endif
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
    if (self.isKeyboardObserve) {
        [self removeKeyboardObserve];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view insertSubview:self.viewBG atIndex:0];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)addObserveOfKeyboard{
    self.isKeyboardObserve = true;
}

#pragma mark - 请求过程回调
- (void)protocolWillRequest{
    [self showLoadingView];
}
- (void)showLoadingView{
    [self.loadingView hideLoading];
    if (self.isNotShowLoadingView) {
        return;
    }
    UIWindow * window = [UIApplication sharedApplication].windows.firstObject;
    [window endEditing:true];
    [self.loadingView resetFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) viewShow:window];
}
- (void)protocolDidRequestSuccess{
    [self.loadingView hideLoading];
}
- (void)protocolDidRequestFailure:(NSString *)errorStr{
    [self.loadingView hideLoading];
    if (self.isNotShowNoticeView) {
        return;
    }
    [GlobalMethod endEditing];
    if ([self.view isShowInScreen]) {
        [self.noticeView showNotice:errorStr time:1 frame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) viewShow:[WindowManager sharedManager].getCurrentWindow];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:true];
    return true;
}

#pragma mark - 改变statusbar颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
    return [GlobalData sharedInstance].statusBarStyle;
}
@end
